<?php


namespace app\home\model;


use think\Db;

class ApprovalUser extends Base
{

    /**
     * 获取审批列表
     * @param int $type 查询类型2申请，3审批
     * @param $userid 用户id
     * @param int $length 加载更多长度
     * @param int $query_type 查询审批类型，0全部
     * @param string $status 查询状态，''空全部
     * @param string $query_time 查询时间
     * @param string $search 模糊搜索
     * @return false|mixed|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList($type = 2,$userid,$length = 0,$query_type = 0,$status = '',$query_time = '',$search = '')
    {
        if($type == 2) {
            if($status == '') {
                $map_status = "`status` >= 0";
            }else {
                $map_status = "`status` = ".$status;
            }
            if($query_time) {
                $thismonth = date('m',$query_time);
                $thisyear = date('Y',$query_time);
                $startDay = $thisyear . '-' . $thismonth . '-1'." 00:00:00";
                $endDay = $thisyear . '-' . $thismonth . '-' . date('t', $query_time)." 23:59:59";
                $start_time =  strtotime($startDay);//当前月的月初时间戳
                $end_time = strtotime($endDay);//当前月的月末时间戳
                $sql_time = " and apply_time between ".$start_time." and ".$end_time;
            }else {
                $sql_time = "";
            }
            switch ($query_type) {
                case 1:
                    $query = "select id,class,'' as back_title,'请假审批' as title,apply_user,apply_time,status from zm_approval_leave where `apply_user` = '".$userid."' and ".$map_status.$sql_time." ORDER BY `apply_time` desc limit ".$length.",10"; //请假审批
                    break;
                case 2:
                    $query = "select id,class,'公章使用审批' as back_title,title,apply_user,apply_time,status from zm_approval_seal where `apply_user` = '".$userid."' and ".$map_status.$sql_time." ORDER BY `apply_time` desc limit ".$length.",10"; //公章使用审批
                    break;
                case 3:
                    $query = "select id,class,'物资采购审批（内购）' as back_title,title,apply_user,apply_time,status from zm_approval_purchase where `apply_user` = '".$userid."' and ".$map_status.$sql_time." ORDER BY `apply_time` desc limit ".$length.",10"; //物资采购审批（内购）
                    break;
                case 4:
                    $query = "select id,class,'部门联系单' as back_title,title,apply_user,apply_time,status from zm_approval_department where `apply_user` = '".$userid."' and ".$map_status.$sql_time." ORDER BY `apply_time` desc limit ".$length.",10"; //部门联系单
                    break;
                case 5;
                    $query = "select id,class,'' as back_title,'加班申请审批' as title,apply_user,apply_time,status from zm_approval_overtime where `apply_user` = '".$userid."' and ".$map_status.$sql_time." ORDER BY `apply_time` desc limit ".$length.",10"; //加班申请
                    break;
                default:
                    $union_all = " union all ";
                    $class1_sql = "select id,class,'' as back_title,'请假审批' as title,apply_user,apply_time,status from zm_approval_leave where `apply_user` = '".$userid."' and ".$map_status.$sql_time; //请假审批
                    $class2_sql = "select id,class,'公章使用审批' as back_title,title,apply_user,apply_time,status from zm_approval_seal where `apply_user` = '".$userid."' and ".$map_status.$sql_time; //公章使用审批
                    $class3_sql = "select id,class,'物资采购审批（内购）' as back_title,title,apply_user,apply_time,status from zm_approval_purchase where `apply_user` = '".$userid."' and ".$map_status.$sql_time; //物资采购审批（内购）
                    $class5_sql = "select id,class,'' as back_title,'加班申请审批' as title,apply_user,apply_time,status from zm_approval_overtime where `apply_user` = '".$userid."' and ".$map_status.$sql_time; //加班申请
                    $class4_sql = "select id,class,'部门联系单' as back_title,title,apply_user,apply_time,status from zm_approval_department where `apply_user` = '".$userid."' and ".$map_status.$sql_time." ORDER BY `apply_time` desc limit ".$length.",10"; //部门联系单
                    $query = $class1_sql.$union_all.$class2_sql.$union_all.$class3_sql.$union_all.$class5_sql.$union_all.$class4_sql;
                    break;
            }
            $list = Db::query($query);
        }else {
            if($status == '') {
                $map = [
                    'approval_user' => $userid,
                    'status' => ['egt',0]
                ];
            }else {
                $map = [
                    'approval_user' => $userid,
                    'status' => $status
                ];
            }
            if($query_type != 0) {
                $map['class'] = $query_type;
            }
            if($query_time) {
                $thismonth = date('m',$query_time);
                $thisyear = date('Y',$query_time);
                $startDay = $thisyear . '-' . $thismonth . '-1'." 00:00:00";
                $endDay = $thisyear . '-' . $thismonth . '-' . date('t', $query_time)." 23:59:59";
                $start_time =  strtotime($startDay);//当前月的月初时间戳
                $end_time = strtotime($endDay);//当前月的月末时间戳
                $map['approval_time'] = ["between",[$start_time,$end_time]];
            }
            $list = Db::table("zm_approval_user")->where($map)->order('id desc')->limit($length,10)->select();
            foreach ($list as $key => $value) {
                $info = $this->getClassMsg($value['class']);
                $apply = Db::table($info['table'])->where(['id' => $value['pid']])->find();
                if($apply) {
                    $user = WechatUser::where('userid',$apply['apply_user'])->find();
                    $list[$key]['title'] = isset($apply['title']) ? $apply['title'] : $info['title'];
                    $list[$key]['apply_time'] = $apply['apply_time'];
                    $list[$key]['apply_user'] = $user ? $user['name'] : "无该用户";
                }
            }
        }
        return $list;
    }

    private function getClassMsg($class) {
        switch ($class) {
            case 1:
                $table = "zm_approval_leave";
                $class_text = "请假审批";
                break;
            case 2:
                $table = "zm_approval_seal";
                $class_text = "公章使用审批";
                break;
            case 3:
                $table = "zm_approval_purchase";
                $class_text = "物资采购审批（内购）";
                break;
            case 4:
                $table = "zm_approval_department";
                $class_text = "部门联系单";
                break;
            case 5:
                $table = "zm_approval_overtime";
                $class_text = "加班申请审批";
                break;
            default:
                return $this->error("无对应类别数据，请重新检查！");
                break;
        }
        $data = [
            'table' => $table,
            'title' => $class_text,
        ];
        return $data;

    }
}